using System;
using System.IO;

namespace Greg.XmlEditor.Business.Concrete
{
	public class TransformationService : ITransformationService
	{
		public IOperationResult Transform(string xmlString, string xmlStylePath)
		{
			var result = new OperationResult();

			try
			{
				// transform the document
				var output = XmlTransformer.Transform(xmlString, xmlStylePath);

				// writes output in cache
				var outputFileName = Path.GetTempFileName();
				File.WriteAllText(outputFileName, output);

				result.Succeeded = true;
				result.Message = outputFileName;

				return result;
			}
			catch (Exception ex)
			{
				result.Succeeded = false;
				result.Message = "Error while applying XSL transformation: " + Environment.NewLine + ex.Message;
				return result;
			}
		}
	}
}